//app.js

import HttpResource from 'helpers/HttpResource'
import WxService from 'helpers/WxService'
import WxValidate from 'helpers/WxValidate'

App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    this.getUserInfo()
  },
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function (login_res) {
          wx.getUserInfo({
            success: function (info_res) {
              that.globalData.userInfo = info_res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)

              wx.request({
                url: 'https://wx.lemna.top/auth',
                data: {
                  code: login_res.code,
                  nickname: info_res.userInfo.nickName
                },
                success: function (auth_res) {
                  wx.setStorageSync('token',auth_res.data)
                }
              })
            }
          })
        }
      })
    }
  },
  globalData: {
    userInfo: null
  },
	WxValidate: (rules, messages) => new WxValidate(rules, messages),
	WxService: new WxService,
  HttpResource: (url, paramDefaults, actions, options) => new HttpResource(url, paramDefaults, actions, options).init()
})
